// vim:syntax=javascript

// Helper function to toggle frame
function toggle_frame(map)
{
    if (map.getFrameType() == "NoFrame" ) {
        map.setFrameType ("RoundedRectangle");
	} else
		map.setFrameType ("NoFrame");
}
// Macro F1: Color subtree red
function macro_f1()
{
    map = vym.currentMap();
    map.colorSubtree ("#ff0000");
}

// Macro F2: Color subtree amber
function macro_f2()
{
    map = vym.currentMap();
    map.colorSubtree ("#d95100");
}

// Macro F3: color subtree green
function macro_f3()
{
    map = vym.currentMap();
    map.colorSubtree ("#009900");
}

// Macro F4: color subtree light green
function macro_f4() 
{
    map = vym.currentMap();
    map.colorSubtree ("#00aa7f");
}

// Macro F5: color subtree purple
function macro_f5()
{
    map = vym.currentMap();
    map.colorSubtree ("#aa00ff");
}

// Macro F6: color subtree light purple
function macro_f6()
{
    map = vym.currentMap();
    map.colorSubtree ("#c466ff");
}

// Macro F7: color subtree blue
function macro_f7()
{
    map = vym.currentMap();
    map.colorSubtree ("#0000ff");
}

// Macro F8: Repeat last command
function macro_f8()
{
    map = vym.currentMap();
    map.repeatLastCommand();
}

// Macro F9: Set task to sleep until 15:00
function macro_f9()
{
    map = vym.currentMap();
    map.setTaskSleep("15:00");
    statusMessage("Task sleeping until 15:00");
}

// Macro F10: Set task to sleep until 18:00
function macro_f10()
{
    map = vym.currentMap();
    map.setTaskSleep("18:00");
    statusMessage("Task sleeping until 18:00");
}

// Macro F11: Set task to sleep until next day
function macro_f11()
{
    map = vym.currentMap();
    map.setTaskSleep("1");
    statusMessage("Task sleeping until tomorrow");
}

// Macro F12: toggle high prio task
function macro_f12()
{
	// Assuming 3 states, which are cycled:
	// 0 - nothing set
	// 1 - high prio task with arrows
	// 2 - done task without arrows, but green hook
    map = vym.currentMap();
    if (map.hasTask() ) {
        // Switch to state 2 
        map.toggleTask();
        map.unsetFlagByName("2arrow-up");
        map.unsetFlagByName("stopsign");
        map.setFlagByName("hook-green");
        map.colorBranch("#0000ff");
    } else {	  
        if (map.hasActiveFlag("hook-green")) {
            // Switch to state 0
            map.unsetFlagByName("hook-green");
        } else {
            // Switch to state 1
            map.setFlagByName("2arrow-up");
            map.toggleTask();
        }
    }
}


// Macro Shift + F1: Frame background light red
function macro_shift_f1()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light red";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#ffb3b4");
    statusMessage(status);
}

// Macro Shift + F2: Frame background light green
function macro_shift_f2()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light green";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#bdffd6");
    statusMessage(status);
}

// Macro Shift + F3: Frame background light yellow
function macro_shift_f3()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light yellow";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#efefb3");
    statusMessage(status);
}

// Macro Shift + F4: Frame background light blue
function macro_shift_f4()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light blue";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#e2e6ff");
    statusMessage(status);
}

// Macro Shift + F5: Frame background light grey
function macro_shift_f5()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light grey";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#d6d6d6");
    statusMessage(status);
}

// Macro Shift + F6: Frame background purple
function macro_shift_f6()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background purple";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#ffaaff");
    statusMessage(status);
}

// Macro Shift + F7: Frame background white
function macro_shift_f7()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background white";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#ffffff");
    statusMessage(status);
}

// Macro Shift + F8: Frame background black
function macro_shift_f8()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background black";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#000000");
    statusMessage(status);
}

// Macro Shift + F9: Toggle frame to include children
function macro_shift_f9()
{
    map = vym.currentMap();
    map.toggleFrameIncludeChildren();
}

// Macro Shift + F10: 
// Useful for timestamps created on last entry
function macro_shift_f10()
{   
}

// Macro Shift + F11: Replace "@..." by mutt aliases
function macro_shift_f11()
{
	map     = vym.currentMap();
	lines   = vym.loadFile("/home/uwe/.mutt/eb-aliases").split('\n');


	if (map.getHeadingPlainText() == "parts") {
		map.setHeadingPlainText("Participants");
	}

	map.initIterator("foobar");

	while (map.nextIterator("foobar") ) {
		heading = map.getHeadingPlainText();

		// Search for "@alias" at beginning of heading
		if (heading.search(/^@\w/) == 0) {
			alias_map = heading.replace(/@/,"");

			for(var i = 0;i < lines.length;i++){
				if (lines[i].search(/^alias/) == 0) {
					if (lines[i].search(alias_map) == 6) {
						name = lines[i].replace(/^.+?\"/, "");
						name = name.replace(/\".+/, "");

						email = lines[i].replace(/^.+?</, "");
						email = email.replace(/>.*/, "");

						map.setHeadingPlainText(name);
					}
				}
			}
		} else {
			print ("No alias found");
        }
	}
}

// Macro Shift + F12: Strip URLs from confluence and generate readable heading
function macro_shift_f12()
{
    vym.clearConsole();
    m1 = vym.currentMap();

    if (vym.isConfluenceAgentAvailable() ) {
        m1.setHeadingConfluencePageName();
        statusMessage("Contacting Confluence...");
        return;
    } 

    org = m1.getURL();

    if (org.length == 0) {
		// No URL yet
		org = m1.getHeadingPlainText();
		print ("No URL yet");
		print ("org: " + org);
		m1.setURL(org);
	}
    print ("Original:");
    print (org);

    s = org;

    // Chop trailing "/"
    if (s[s.length-1] == '/') {
      s = s.slice(0, s.length - 1);
    }

    // Remove confluence parts
    if (s.indexOf("infohub") != -1) {
      print("Found infohub page");
      if (s.indexOf("spaceKey") != -1) {
        s = s.replace(/^.*spaceKey=/g, "");
        s = s.replace(/(&title=)/g, ": ");
      } else
      {
        s = s.replace(/^.*display\//g, "");
        s = s.replace(/\//g, ": ");
      }
    } else // no infohub page
    {
      // remove path
      s = s.replace(/^.*\//g, "");
    }

    print (s);

    // Replace "+" and "-"
    s = s.replace(/\+/g, " ");
    s = s.replace(/%3A/g, "");
    print ("After replacing:");
    print (s);

    // Write new header
    m1.setHeadingPlainText(s);

    if ( org == s ) {
      statusMessage ("Heading not changed.");
    } else {
      statusMessage("Changed heading: " + org + " -> " + s);
    }
}